package com.lbw.controller;

import com.sun.org.apache.regexp.internal.RE;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author Mr. Liu
 * @Date 2023/3/25 16:15
 */
@RestController
public class AController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;
    Random random=new Random();

    private int requestCount=0;

    AtomicInteger ai=new AtomicInteger();
    /**
     * 浏览器 -----A(/dis?serviceName=x)----服务
     * @param serviceName
     * @return
     */
    @GetMapping("dis")
    public String discovery(String serviceName){
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        System.out.println(host+":"+port);
        String url="http://"+host+":"+port+"/info";
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }

    @GetMapping("myLb")
    public String myLb(String serviceName){
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        if (CollectionUtils.isEmpty(instances)){
            return "没有此服务";
        }
        ServiceInstance serviceInstance=myChoose(instances);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        String url="http://"+host+":"+port+"/info";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * 自己做负载均衡算法
     * 轮询
     * iphash
     * 权重
     * 随机
     *
     * @param instances
     * @return
     */
    private ServiceInstance myChoose(List<ServiceInstance> instances) {
        //随机
//        ServiceInstance serviceInstance = instances.get(random.nextInt(instances.size()));
        //权重 第一个机器分配 20% 第二个机器分配80%
//        int i = random.nextInt(5);
//        if (i<=3){
//            return instances.get(1);
//        }
//        return instances.get(0);
        //iphash 固定的ip去固定的机器上 每一个请求 就是一个request
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        String ip = request.getRemoteAddr();
//        int ipHash = ip.hashCode();
//        int index = ipHash % instances.size();
//        return instances.get(index);
        //轮询
        int position = ai.incrementAndGet();
        int index = position % instances.size();
        return instances.get(index);
    }

    @GetMapping("lb")
    public String lb(String serviceName){
        return restTemplate.getForObject("http://"+serviceName+"/info",String.class);
    }
}
